from PySide2.QtWidgets import QApplication,QMainWindow
from PySide2 import QtCore,QtGui
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from QT_chatuiside2 import Ui_MainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        pixmap1 = QtGui.QPixmap('机器人.jpg')  # 导入本地图片
        self.ui.label_p1.setPixmap(pixmap1)  # 设置图片到label
        self.ui.label_p1.setScaledContents(True)  # l图片自适应

        pixmap2 = QtGui.QPixmap('白熊.jpg')  # 导入本地图片
        self.ui.label_p2.setPixmap(pixmap2)  # 设置图片到label
        self.ui.label_p2.setScaledContents(True)  # l图片自适应



        self.train_robor()
        global Nlp_robot
        self.ui.pushButton.clicked.connect(self.sendmsg)

    def train_robor(self):
        # 创建聊天机器人实例
        global Nlp_robot
        Nlp_robot = ChatBot('NLP_robot',
                            storage_adapter='chatterbot.storage.SQLStorageAdapter',
                            database_uri='sqlite:///database.sqlite3_eng',
                            logic_adapters=[
                                'chatterbot.logic.MathematicalEvaluation',
                                'chatterbot.logic.BestMatch',
                                {
                                    'import_path': 'chatterbot.logic.BestMatch',
                                    'default_response': '对不起我不能理解你所说的，请给我一点时间学习或者联系。。。',
                                    'maximum_similarity_threshold': 0.70
                                }],
                            read_only=True,
                            )

        trainer_corpus = ChatterBotCorpusTrainer(Nlp_robot)
        trainer_corpus.train('chatterbot.corpus.chinese')

        self.ui.label_1.setText('你好！我是小N，让我们开始聊天吧！')

    def sendmsg(self):
        global Nlp_robot

        request = self.ui.msgsendbox.toPlainText()
        if request == "拜拜" or request == '退出':
            self.ui.label_1.setText('期待和你的再次见面')
            self.ui.msgsendbox.clear()


        else:
            response = Nlp_robot.get_response(request)
            self.ui.label_1.setText(str(response))
            self.ui.label_2.setText(str(request))
            self.ui.msgsendbox.clear()




if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()